#!/bin/bash

##### Constants

API_KEY=logclient
SECRET_KEY=11235813
#URL=http://localhost:8080/log
URL=http://logservertest.appspot.com/log
CHK_FILE=tmp.sha1

##### Functions

function usage
{
    echo "usage: log -m message [-s severity] [-p app_name]"
}

##### Main

message=
severity=INFO
app_name=

while [ "$1" != "" ]; do
    case $1 in
        -m | --message )     shift
                             message=$1
                             ;;
        -s | --severity )    shift
                             severity=$1
                             ;;
        -p | --app_name )    shift
                             app_name=$1
                             ;;
        -h | --help )        usage
                             exit
                             ;;
        * )                  usage
                             exit 1
    esac
    shift
done

if [ "$message" = "" ]
then
    usage
	exit 1
fi

Nonce=$RANDOM

CHK_FILE=tmp.sha1
echo -n m"$message"n"$Nonce"p"$app_name"s"$severity$SECRET_KEY" 1>$CHK_FILE
Sig=`sha1sum $CHK_FILE`
rm -f $CHK_FILE

curl -d n="$Nonce" -d s="$severity" -d m="$message" -d p="$app_name" -d key="$API_KEY" -d sig=${Sig%% *} $URL